package com.wudi.estate.controller.servicemode;

import com.github.pagehelper.PageInfo;
import com.wudi.estate.bean.communitymanagement.Community;
import com.wudi.estate.bean.servicemode.Activity;
import com.wudi.estate.service.communitymanagement.CommunityService;
import com.wudi.estate.service.servicemode.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;
    @Autowired
    private CommunityService communityService;
    //活动列表显示
    @RequestMapping("/listUi")
    public String listUi(
            ModelMap map,
            String title,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date likeBeginTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date likeEndTime,
            Integer pageNum){
        if(pageNum==null){
            pageNum=1;
        }
        if(pageNum==0){
            pageNum=1;
        }
//        List<Map<String,Object>> activities = new ArrayList<>();
        PageInfo<Map<String,Object>> pageInfo = null;
        try {
            if(title==null){
//              activities= activityService.selectActivityAndCommunity();
                pageInfo= activityService.selectActivityAndCommunityPage(pageNum,3);
            }
            else {
//                activities = activityService.selectActivityAndCommunityByLike(title,likeBeginTime,likeEndTime);
                pageInfo= activityService.selectActivityAndCommunityByLikeAndPage(title,likeBeginTime,likeEndTime,pageNum,3);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        map.addAttribute("pageInfo",pageInfo);
        return "activity_list";
    }
//    显示添加活动的页面
    @RequestMapping("/addUi")
    public String addUi(ModelMap map){
        List<Community> communitys = null;
        try {
            communitys = communityService.select();
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.addAttribute("communitys",communitys);
       System.out.println(communitys);
       return "activity_add";
    }
//    添加活动，返回到“活动列表显示”里
    @RequestMapping("/add")
    public String add(Activity activity){//使对象自动注入，id和createTime不需要
        try {
            activityService.insert(activity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("进入到添加逻辑");
        return "redirect:/activity/addUi";
    }
//   活动编辑显示
    @RequestMapping("/editUi")
    public String editUi(ModelMap map,long id){
        try {
           Map<String,Object> activities= activityService.selectActivityAndCommunityById(id);
            System.out.println(activities);
            map.addAttribute("activitys",activities);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "activity_edit";
    }
    //   活动编辑逻辑
    @RequestMapping("/edit")
    public String edit(Activity activity){

        try {
            activityService.update(activity);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "redirect:/activity/listUi";
    }
//删除活动
    @RequestMapping("/delete")
    public String delete(long id){
        try {
            activityService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(id);
        return "redirect:/activity/listUi";
    }
}
